package com.ruoyi.web.controller.credit;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.credit.domain.BaseRules;
import com.ruoyi.credit.service.IBaseRulesService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 制度规则Controller
 *
 * @author zhulei
 * @date 2022-03-10
 */
@Controller
@RequestMapping("/credit/rules")
public class BaseRulesController extends BaseController
{
    private String prefix = "credit/rules";

    @Autowired
    private IBaseRulesService baseRulesService;

    @RequiresPermissions("credit:rules:view")
    @GetMapping()
    public String rules()
    {
        return prefix + "/rules";
    }

    /**
     * 查询制度规则列表
     */
    @RequiresPermissions("credit:rules:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BaseRules baseRules)
    {
        startPage();
        List<BaseRules> list = baseRulesService.selectBaseRulesList(baseRules);
        return getDataTable(list);
    }

    /**
     * 导出制度规则列表
     */
    @RequiresPermissions("credit:rules:export")
    @Log(title = "制度规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseRules baseRules)
    {
        List<BaseRules> list = baseRulesService.selectBaseRulesList(baseRules);
        ExcelUtil<BaseRules> util = new ExcelUtil<BaseRules>(BaseRules.class);
        return util.exportExcel(list, "制度规则数据");
    }

    /**
     * 新增制度规则
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存制度规则
     */
    @RequiresPermissions("credit:rules:add")
    @Log(title = "制度规则", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BaseRules baseRules)
    {
        return toAjax(baseRulesService.insertBaseRules(baseRules));
    }

    /**
     * 修改制度规则
     */
    @RequiresPermissions("credit:rules:edit")
    @GetMapping("/edit/{ruleId}")
    public String edit(@PathVariable("ruleId") Long ruleId, ModelMap mmap)
    {
        BaseRules baseRules = baseRulesService.selectBaseRulesByRuleId(ruleId);
        mmap.put("baseRules", baseRules);
        return prefix + "/edit";
    }

    /**
     * 修改保存制度规则
     */
    @RequiresPermissions("credit:rules:edit")
    @Log(title = "制度规则", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseRules baseRules)
    {
        return toAjax(baseRulesService.updateBaseRules(baseRules));
    }

    /**
     * 删除制度规则
     */
    @RequiresPermissions("credit:rules:remove")
    @Log(title = "制度规则", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(baseRulesService.deleteBaseRulesByRuleIds(ids));
    }
}
